package com.suning.sawp.service.impl.servicenum;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.suning.sawp.constants.CacheKeyConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.intf.servicenum.ServiceNumService;
import com.suning.sawp.service.util.RedisCacheUtils;
import com.suning.vgs.follow.remote.intf.ServiceNumRemoteService;
import com.suning.vgs.follow.remote.store.ServiceNumDto;

@Service
public class ServiceNumServiceImpl implements ServiceNumService {

    @Resource
    ServiceNumRemoteService     serviceNumRemoteService;
    
    @Resource
    RedisCacheUtils  cacheUtils;

    private static final String SEPARATOR = ",";

    @Override
    public ReturnMsg<List<ServiceNumDto>> queryServiceNumInfo(String serviceNumId) {
        ReturnMsg<List<ServiceNumDto>> ret = new ReturnMsg<List<ServiceNumDto>>();
        List<ServiceNumDto> list = serviceNumRemoteService.queryServiceNum(serviceNumId);
        ret.setRetFlag(ReturnMsg.SUCCESS);
        ret.setData(list);
        return ret;
    }

    @Override
    public ReturnMsg<CommonRtn> saveServiceNum(String serviceNumId, String serviceNumName,
            String systemIds) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        if (StringUtils.isBlank(serviceNumName) || StringUtils.isBlank(systemIds)) {
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setError(ErrorCodeConstants.SERVICE_NUM_ERR_001,
                    ErrorCodeConstants.SERVICE_NUM_ERR_MAP
                            .get(ErrorCodeConstants.SERVICE_NUM_ERR_001));
            return ret;
        }
        List<String> sysIdList = new ArrayList<String>();
        String[] split = systemIds.split(SEPARATOR);
        for (String id : split) {
            sysIdList.add(id);
        }
        ServiceNumDto dto = new ServiceNumDto();
        dto.setServiceNumName(serviceNumName);
        dto.setSystemIdList(sysIdList);
        int flag = 0;
        if (StringUtils.isBlank(serviceNumId)) {
            serviceNumId = serviceNumRemoteService.addServiceNum(dto);
            if (StringUtils.isNotBlank(serviceNumId) && !"-1".equals(serviceNumId)) {
               flag = 1;
               cacheUtils.clearCacheData(CacheKeyConstants.CLOUD_GUIDE_IDS);
            } else if ("-1".equals(serviceNumId)) {
               flag = -1;
            }
        } else {
            dto.setServiceNumId(serviceNumId);
            flag = serviceNumRemoteService.updateServiceNum(dto, serviceNumId);
        }
        if(flag > 0){
            ret.setRetFlag(ReturnMsg.SUCCESS);
        }else if(flag == -1){
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setError(ErrorCodeConstants.SERVICE_NUM_ERR_003,
                    ErrorCodeConstants.SERVICE_NUM_ERR_MAP
                            .get(ErrorCodeConstants.SERVICE_NUM_ERR_003));
        }else{
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setError(ErrorCodeConstants.SERVICE_NUM_ERR_002,
                    ErrorCodeConstants.SERVICE_NUM_ERR_MAP
                            .get(ErrorCodeConstants.SERVICE_NUM_ERR_002));
        }
        return ret;
    }

    @Override
    public List<String> queryServiceNumCloudGuideIds() {
        Type type = new TypeToken<List<String>>() {
        }.getType();
        List<String> cloudGuideList = cacheUtils.hget(CacheKeyConstants.CLOUD_GUIDE_IDS,CacheKeyConstants.CLOUD_GUIDE_IDS, type);
        if(cloudGuideList == null){
            cloudGuideList = new ArrayList<String>();
            List<ServiceNumDto> list = serviceNumRemoteService.queryServiceNum("");
            if(CollectionUtils.isNotEmpty(list)){
                List<String> staffIds = new ArrayList<String>();
                for(ServiceNumDto dto: list){
                    staffIds.add(dto.getServiceNumId());
                }
                cloudGuideList = serviceNumRemoteService.queryCloudGuideId(staffIds);
            }
            cacheUtils.hset(CacheKeyConstants.CLOUD_GUIDE_IDS, CacheKeyConstants.CLOUD_GUIDE_IDS, cloudGuideList);
        }
        return cloudGuideList;
    }

}
